package com.kizuki.config;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 想要得要两个RabbitTemplate
 */
@Configuration
public class RabbitMqConfiguration {

    // 读取springboot配置
    @Bean(name = "rabbitmqConnection")
    public ConnectionFactory connectionFactory(
            @Value("${spring.rabbitmq.host}") String host,
            @Value("${spring.rabbitmq.port}") int port,
            @Value("${spring.rabbitmq.username}") String username,
            @Value("${spring.rabbitmq.password}") String password) {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setHost(host);
        cachingConnectionFactory.setPort(port);
        cachingConnectionFactory.setUsername(username);
        cachingConnectionFactory.setPassword(password);
        return cachingConnectionFactory;
    }

    @Bean(name = "ttlRabbitTemplate")
    public RabbitTemplate getTtlRabbitTemplate(@Qualifier("rabbitmqConnection") ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }

    @Bean(name = "confirmRabbitTemplate")
    public RabbitTemplate getConfirmRabbitTemplate(@Qualifier("rabbitmqConnection") ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }

}
